import requests
from requests.exceptions import RequestException
import re


def get_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            response.encoding = 'utf-8'
            return response.text
        return None
    except RequestException:
        return None


def get_movie_list(html):
    pattern = re.compile('<div class="pic".*?em class="">(.*?)</em>.*?<a href="(.*?)">.*?</a>', re.S)
    movie_list = pattern.findall(html)
    return movie_list
    

def get_content(movie_url):
    html = get_page(movie_url)
    html = str(html)
    # 获取片名
    pattern = re.compile('<span property="v:itemreviewed">(.*?)</span>', re.S)
    name = pattern.findall(html)
    print(name)
    # 导演
    pattern = re.compile('<a.*?rel="v:directedBy">(.*?)</a>', re.S)
    director = pattern.findall(html)
    print(director)

    # 编剧 没办法一次解析出来 可以解析两次 第一次缩小范围
    pattern = re.compile("<span ><span class='pl'>编剧</span>: <span class='attrs'>(.*?)</span></span><br/>", re.S)
    author = pattern.findall(html)
    if author:
        pattern = re.compile('<a href=.*?>(.*?)</a>', re.S)
        author = pattern.findall(author[0])
    print(author)

    # 主演
    pattern = re.compile('<a.*?rel="v:starring">(.*?)</a>', re.S)
    actor = pattern.findall(html)
    print(actor)

    # 类型
    pattern = re.compile('<span property="v:genre">(.*?)</span>', re.S)
    type = pattern.findall(html)
    print(type)

    # 制片国家/地区
    pattern = re.compile('<span class="pl">制片国家/地区:</span>(.*?)<br/>', re.S)
    area = pattern.findall(html)
    print(area)

    # 语言
    pattern = re.compile('<span class="pl">语言:</span>(.*?)<br/>', re.S)
    language = pattern.findall(html)
    print(language)

    # 上映时间
    pattern = re.compile('<span property="v:initialReleaseDate" content=.*?>(.*?)</span>', re.S)
    time = pattern.findall(html)
    print(time)

    # 片长
    pattern = re.compile('<span property="v:runtime" content=.*?>(.*?)</span>', re.S)
    runtime = pattern.findall(html)
    print(runtime)

    # 又名
    pattern = re.compile('<span class="pl">又名:</span>(.*?)<br/>', re.S)
    other_name = pattern.findall(html)
    print(other_name)

    # 评分
    pattern = re.compile('<strong class="ll rating_num" property="v:average">(.*?)</strong>', re.S)
    score = pattern.findall(html)
    print(score)

    # 简介
    pattern = re.compile('<span property="v:summary".*?>(.*?)</span>', re.S)
    introduce = pattern.findall(html)

    introduce = introduce[0].strip().replace('\n', '').replace('\t', '').replace(
        '                                    <br />                                　　', '')
    print(introduce)

    # 保存数据
    with open('result.txt', 'a', encoding='utf-8') as f:
        f.write(movie[0] + '\t' + movie[1] + '\t' + str(name) + '\t' + str(director) + '\t' + str(author) + '\t' + str(
            actor) + '\t' + str(type) + '\t' + str(area) + '\t' + str(language) + '\t' + str(
            time) + '\t' + str(runtime) + '\t' + str(other_name) + '\t' + str(
            score) + '\t' + introduce + '\n')


if __name__ == '__main__':
    for i in range(10):
        url = 'https://movie.douban.com/top250?start=' + str(i * 25)
        print(url)
        # 发送请求 获取响应
        html = get_page(url)
        # print(html)
        # 解析响应 获取电影列表
        movie_list = get_movie_list(html)
        print(movie_list)
        # 获取每部电影的详细内容
        for movie in movie_list:
            get_content(movie[1])